<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\Products;

use App\Jobs\Products\AmazonSkuUpRelate;
use App\Models\Product\ProductAmazonSku;
use App\Models\Stocking\StockingSku;
use App\Models\Shipping\SSku;
use App\Models\Basics\SysICountry;
use Illuminate\Support\Facades\DB;
class AmazonSkuAjax {

    /**
     * 停用
     * @param $ids
     * @return \Illuminate\Http\JsonResponse
     */
    public function disableItem($ids){
        try{
            $collection = ProductAmazonSku::query()->findMany($ids);
            $count = 0;
            $area = [];
            $areaPskuArr = [];
            foreach ($collection as $model) {
                DB::beginTransaction();
                if($model->status == 2){
                    DB::rollBack();
                    continue;
                }
                $model->status = 2;
                if($model->times != 0){
                    $model->times = 0;
                }
                $flag = $model->save();
                if(!$flag){
                    DB::rollBack();
                }else{
                    $count++;
                    DB::commit();
                    if(!array_key_exists($model->country_id,$area)){
                        $area[$model->country_id] = SysICountry::getAreaIdByCountryId($model->country_id);
                    }
                    $_areaPsku = $area[$model->country_id].'_'.$model->psku_id;
                    if(!in_array($_areaPsku,$areaPskuArr)){
                        $areaPskuArr[] = $_areaPsku;
                    }
                }
            }
            if(!empty($areaPskuArr)){
                $country = [];
                foreach ($areaPskuArr as $areaPsku) {
                    list($areaId,$pSkuId) = explode('_',$areaPsku);
                    if(!array_key_exists($areaId,$country)){
                        $country[$areaId] = SysICountry::getCountryIdsByAreaId($areaId);
                    }
                    $countryIds = $country[$areaId];
                    $count = ProductAmazonSku::query()
                            ->where('status',1)
                            ->whereIn('country_id',$countryIds)
                            ->where('psku_id',$pSkuId)
                            ->count();
                    if($count > 0)continue;
                    DB::beginTransaction();
                    $stockingSku = StockingSku::query()->where('area_psku',$areaPsku)->first();
                    if($stockingSku){
                        $stockingSku->enable_status = 0;//正常逻辑是应该直接删掉，但是为了程序的简化，万一点错了不小心点了亚马逊产品sku的停用动作，需要重新启用。
                        $stockingSku->save();
                    }
                    $sSku = SSku::query()->where('area_psku',$areaPsku)->first();
                    if($sSku){
                        $sSku->enable_status = 0;
                        $sSku->save();
                    }
                    DB::commit();
                }
            }
            return response()->json(['status' => true, 'message' => "成功停用{$count}条记录！"]);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * 启用
     * @param $ids
     * @return \Illuminate\Http\JsonResponse
     */
    public function enableItem($ids){
        try{
            $collection = ProductAmazonSku::query()->findMany($ids);
            $count = 0;
            $items = [];
            $area = [];
            $areaPskuArr = [];
            foreach ($collection as $model) {
                DB::beginTransaction();
                if($model->status == 1){
                    DB::rollBack();
                    continue;
                }
                $model->status = 1;
                $flag = $model->save();
                if(!$flag){
                    DB::rollBack();
                }else{
                    $count++;
                    DB::commit();
                    if(!array_key_exists($model->country_id,$area)){
                        $area[$model->country_id] = SysICountry::getAreaIdByCountryId($model->country_id);
                    }
                    $_areaPsku = $area[$model->country_id].'_'.$model->psku_id;
                    if(!in_array($_areaPsku,$areaPskuArr)){
                        $areaPskuArr[] = $_areaPsku;
                    }
                    if($model->fan_eu == 1){
                        $fanEuKey = $model->seller_sku.'_'.$model->child_asin.'_'.$model->fnsku;
                        if(!in_array($fanEuKey,$items)){
                            $items[] = $fanEuKey;
                        }
                    }
                }
            }
            if(!empty($areaPskuArr)){
                foreach ($areaPskuArr as $areaPsku) {
                    DB::beginTransaction();
                    $stockingSku = StockingSku::query()->where('area_psku',$areaPsku)->first();
                    if($stockingSku){
                        if($stockingSku->enable_status != 1){
                            $stockingSku->enable_status = 1;
                            $stockingSku->save();
                        }
                    }
                    $sSku = SSku::query()->where('area_psku',$areaPsku)->first();
                    if($sSku){
                        if($sSku->enable_status != 1){
                            $sSku->enable_status = 1;
                            $sSku->save();
                        }
                    }
                    DB::commit();
                }
            }
            if(!empty($items)){
                AmazonSkuUpRelate::dispatch($items);
            }
            return response()->json(['status' => true, 'message' => "成功启用{$count}条记录！"]);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }
}
